#coding:utf-8
from django.template import RequestContext
from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render
import json

import common.httpclient as http
import common.tips as tips
import common.modelAdminUser as modelAdminUser
import config.myurls as myurls



def list(req):
  '银行限额配置'
  
  main_nav=[]
  context={}
  context['title'] = '-银行限额配置'
  
  if modelAdminUser.getLoginUserId(req)==0:
    return HttpResponseRedirect('/login')
  
  url=myurls.ApiGetBanklimitList;
  
  param=[];
  rt=[]
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  context['list']=rt['list']
  
  
  main_nav.append({"url":"","name":"银行限额配置("+str(rt['count'])+")"})
  context['main_nav']=main_nav
  
  return render(req, 'banklimit_list.html', context)

def edit(req):
  '银行限额配置编辑'
  
  a=""
  
  id=0
  try:
    id=int(str(req.GET.get("id")))
  except ValueError:
    pass
  
  if id==0:
    return tips.error(req,"id不能为0","")
  
  main_nav=[]
  main_nav.append({"url":"/banklimit/","name":"银行限额列表"})
  main_nav.append({"url":"","name":"银行限额配置编辑"})
  context={}
  context['title'] = '-银行限额配置编辑'
  context['main_nav']=main_nav
  
  url=myurls.ApiGetBanklimitInfo;
  
  param=[];
  param.append(("id",id))
  rt=[]
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  
  context['info']=json.loads(rt['info'])
  return render(req, 'banklimit_edit.html', context)


def editpost(req):
  '提交编辑'
  
  rt={}
  rt['response_code']=0
  rt['response_message']="待提交"
  
  param=[]
  param.append(("id",req.POST.get("id")))
  
  
  for key in req.POST:
    param.append((key,req.POST.get(key)))
  
  url=myurls.ApiUpdBanklimitInfo;
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  return tips.returnAjax(rt)
  #return HttpResponse(json.dumps(rt),content_type="application/json")
  #return tips.success(req,"提交成功","/banklimit/edit/?id="+req.POST.get("id"))
  #return HttpResponse(str(rt))